package com.lzp.workframe.mapStruct;

import com.lzp.entity.WechatBill;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author : lzp
 * @version 1.0
 * @date : 2022/8/11 19:42
 * @apiNote : 映射类
 */
@Component
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface WechatBillStruct {
    default WechatBill weChatFieldSet2Entity(FieldSet fieldSet) {
        WechatBill wechatBill = new WechatBill();
        Date tradingTimeStr = fieldSet.readDate("交易时间");
        Instant instant = tradingTimeStr.toInstant();
        LocalDateTime tradingTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        wechatBill.setTradingTime(tradingTime);

        String tradingType = fieldSet.readString("交易类型");
        wechatBill.setTradingType(tradingType);

        String counterparty = fieldSet.readString("交易对方");
        wechatBill.setCounterparty(counterparty);
        String goods = fieldSet.readString("商品");
        wechatBill.setGoods(goods);

        String revenueExpenditure = fieldSet.readString("收/支");
        wechatBill.setRevenueExpenditure(revenueExpenditure);

        String moneyAmount = fieldSet.readString("金额(元)");
        //在读取的时候就规范格式化金额
        if (moneyAmount != null) {
            moneyAmount = moneyAmount.replaceAll("¥", "");
        }
        wechatBill.setMoneyAmount(moneyAmount);

        String paymentMethod = fieldSet.readString("支付方式");
        wechatBill.setPaymentMethod(paymentMethod);

        String status = fieldSet.readString("当前状态");
        wechatBill.setStatus(status);

        String transactionNo = fieldSet.readString("交易单号");
        wechatBill.setTransactionNo(transactionNo);

        String merchantNo = fieldSet.readString("商户单号");
        wechatBill.setMerchantNo(merchantNo);

        String remarks = fieldSet.readString("备注");
        wechatBill.setRemarks(remarks);
        return wechatBill;
    }

    default WechatBill alipayField2Entity(FieldSet fieldSet) {

        WechatBill wechatBill = new WechatBill();
        Date tradingTimeStr = fieldSet.readDate("交易时间");
        Instant instant = tradingTimeStr.toInstant();
        LocalDateTime tradingTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        wechatBill.setTradingTime(tradingTime);

        String tradingType = fieldSet.readString("交易分类");
        wechatBill.setTradingType(tradingType);

        String counterparty = fieldSet.readString("交易对方");
        wechatBill.setCounterparty(counterparty);
        String goods = fieldSet.readString("商品说明");
        wechatBill.setGoods(goods);

        String revenueExpenditure = fieldSet.readString("收/支");
        wechatBill.setRevenueExpenditure(revenueExpenditure);

        String moneyAmount = fieldSet.readString("金额");
        wechatBill.setMoneyAmount(moneyAmount);

        String paymentMethod = fieldSet.readString("收/付款方式");
        wechatBill.setPaymentMethod(paymentMethod);

        String status = fieldSet.readString("交易状态");
        wechatBill.setStatus(status);

        String transactionNo = fieldSet.readString("交易订单号");
        wechatBill.setTransactionNo(transactionNo);

        String merchantNo = fieldSet.readString("商家订单号");
        wechatBill.setMerchantNo(merchantNo);

        String remarks = fieldSet.readString("对方账号");
        wechatBill.setRemarks(remarks);
        return wechatBill;
    }
}
